package com.csii.branch.activity.server.impl;

import com.csii.activiti.server.abstraction.entity.ActUser;
import com.csii.activiti.server.abstraction.service.ActUserService;
import com.csii.core.api.Result;
import com.csii.user.entity.SysUser;
import com.csii.user.service.ISysUserBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ActUserServiceImpl implements ActUserService {

    @Autowired
    private ISysUserBaseService sysUserBaseService;

    @Override
    public List<ActUser> queryUserLikeUsernameOrNickname(String usernameOrNickname) {
        Result<List<SysUser>> listResult = sysUserBaseService.queryUserLikeUsernameOrNickname(usernameOrNickname);
        if (listResult != null){
            List<SysUser> result = listResult.getResult();
            return result.stream().map(item-> new ActUser(item.getUsername(),item.getRealname())).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<ActUser> queryUserByRoleCode(String roleCode) {
        Result<List<SysUser>> listResult = sysUserBaseService.queryUserByRoleCode(roleCode);
        if (listResult != null){
            List<SysUser> result = listResult.getResult();
            return result.stream().map(item-> new ActUser(item.getUsername(),item.getRealname())).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<ActUser> queryUserByUsernames(Set<String> usernames) {
        Result<List<SysUser>> listResult = sysUserBaseService.queryUserByUsernames(new ArrayList<>(usernames));
        if (listResult != null){
            List<SysUser> result = listResult.getResult();
            return result.stream().map(item-> new ActUser(item.getUsername(),item.getRealname())).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<ActUser> queryHisLeadershipByUsername(String username) {
        Result<List<SysUser>> listResult = sysUserBaseService.queryHisLeadershipByUsername(username);
        if (listResult != null){
            List<SysUser> result = listResult.getResult();
            return result.stream().map(item-> new ActUser(item.getUsername(),item.getRealname())).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
